local game = require "club.threecard.hbsl"
local GameObjectExtension = CS.Blaze.GameObjectExtension
local Vector3 = CS.UnityEngine.Vector3
local TextMeshProUGUIType = typeof(CS.TMPro.TextMeshProUGUI)

local M = game.ui.element()

function M:ctor()
    self.res = "club.threecard.hbsl/ui/ingame.b:packet_dispatcher"
end

function M:dispatch(target)
    self:lazy("on_dispatch", target)
end

function M:to_self(target, amount)
    self:lazy("on_to_self", target, amount)
end

function M:on_dispatch(target)
    local obj = GameObjectExtension.GetFromPool(self.temp)
    obj.transform:SetParent(self.root, false)
    obj.transform.localPosition = Vector3.zero

    obj.transform:DOMove(target.transform.position, 0.3)
    :OnComplete(function()
        GameObjectExtension.ReturnToPool(obj)
    end)

    game.audio.post("hbsl:packet")
end

function M:on_to_self(target, amount)
    local obj = GameObjectExtension.GetFromPool(self.self_temp)
    obj.transform:SetParent(self.root, false)
    obj.transform.localPosition = Vector3.zero

    local txt = obj.transform:Find("amount_txt"):GetComponent(TextMeshProUGUIType)
    if txt then
        txt.text = amount
    end

    obj.transform:DOMove(target.transform.position, 0.3)
    :SetDelay(2)
    :OnComplete(function()
        GameObjectExtension.ReturnToPool(obj)
    end)
end

return M